/**
 * 机构数据表格
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.SysColumnGrid = Ext.extend(Ext.ux.grid.CrudGrid, {

	initComponent: function(){
		var gridStructure = [{ 
			name: 'id',
			hidden: true
		},{ 
			name: 'tableId',
			hidden: true
		},{
			name: 'tableName',  
			header: "所属表",
			sortable: true,
			width: 150
		},{
			name: 'columnName',  
			header: "字段名",
			sortable: true,
			width: 120
		},{
			name: 'columnComments',  
			header: "字段注释",
			width: 150
		},{
			name: 'columnType',  
			header: "字段类型",
			sortable: true,
			width: 150
		},{
			name: 'columnLength',  
			header: "字段长度",
			sortable: true,
			width: 120
		},{
			name: 'columnPrecision',  
			header: "字段精度",
			width: 120
		},{
			name: 'columnScale',  
			header: "小数位",
			width: 120
		},{
			name: 'nullableLabel',  
			header: "是否为空",
			width: 120
		},{
			name: 'isPrimaryKeyLabel',  
			header: "是否主键",
			width: 120
		},{
			name: 'memo',  
			header: "描述",
			hidden: true,
			width: 200
		}];
		
		this.structure = gridStructure;
		this.defaultSortField = 'columnName';
		this.keyField = 'id';

		this.deleteMsg = '确认要删除选中的字段吗？';
		this.deleteMsgBoxWidth = 350;
		
		Ext.ux.grid.SysColumnGrid.superclass.initComponent.call(this);
		
		this.on('rowdblclick', function(grid){
			var record = grid.getSelectionModel().getSelected();
			if(record != null){
				var id = record.get('id');//解析
				this.showColumnInfo(id);
			}
		});
		
		this.on('updateRecord', function(columnId){
			this.showColumnInfo(columnId);
		});
		
		this.on('deleteRecord', function(grid, columnIds){
			this.deleteColumn(columnIds);
		});
	},
	
	showColumnInfo: function(columnId){
		var win = new Ext.ux.sysmeta.SysColumnDetailWindow({id:'columnDetailWindow',columnId:columnId,title:'修改字段信息'});
		win.showColumnInfo();
	},
	
	addColumn: function(tableId, tableName){	
		var win = new Ext.ux.sysmeta.SysColumnDetailWindow({id:'columnDetailWindow', title:'新增字段', tableId:tableId, tableName:tableName});
		win.show();
	},
	
	deleteColumn: function(columnIds) {
		Ext.Ajax.request({
			url:'sys-meta-column!deleteMore.action',
			success: function(){
				Ext.getCmp('treePanel').refresh();
				this.store.reload();
			},
			failure: function(form, action) {
				Ext.Msg.alert('系统提示', '删除表失败！');
			},
			method: 'POST',
			params:{
				ids: columnIds
			},
			waitMsg : '正在删除数据，稍候...',
			scope: this
		});
	}
});